home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / newsgroups / misc.20000217-20000824 / 000038_news@columbia.edu _Tue Feb 22 10:40:25 2000.msg < prev    next >
Internet Message Format  |  2020-01-01  |  3KB

  1. Return-Path: <news@columbia.edu>
  2. Received: from newsmaster.cc.columbia.edu (newsmaster.cc.columbia.edu [128.59.59.30])
  3.     by watsun.cc.columbia.edu (8.8.5/8.8.5) with ESMTP id KAA18658
  4.     for <kermit.misc@watsun.cc.columbia.edu>; Tue, 22 Feb 2000 10:40:25 -0500 (EST)
  5. Received: (from news@localhost)
  6.     by newsmaster.cc.columbia.edu (8.9.3/8.9.3) id KAA15336
  7.     for kermit.misc@watsun.cc.columbia.edu; Tue, 22 Feb 2000 10:20:11 -0500 (EST)
  8. X-Authentication-Warning: newsmaster.cc.columbia.edu: news set sender to <news> using -f
  9. From: fdc@watsun.cc.columbia.edu (Frank da Cruz)
  10. Subject: Re: Array name passed to macro as argument?
  11. Date: 22 Feb 2000 15:20:11 GMT
  12. Organization: Columbia University
  13. Message-ID: <88u9fb$ev5$1@newsmaster.cc.columbia.edu>
  14. To: kermit.misc@columbia.edu
  15.  
  16. In article <88u6ql$vmg$1@nnrp1.deja.com>,
  17. Peter Easthope  <peter_easthope@gulfnet.pinc.com> wrote:
  18. : ; Can the name of an array be passed to a macro via an
  19. : ; argument?  This message can be interpreted as an example
  20. : ; but the syntax fails.  Any suggestions?
  21. : ; ================================================
  22. : ; pass the name of an array to Test as an argument.
  23. : def Test {
  24. :   echo {\%1[1]}
  25. :   echo {\%1[2]}
  26. : }
  27. : declare \&d[2]
  28. : def \&d[1] {Sea}
  29. : def \&d[2] {urchin}
  30. :
  31. By the way, in C-Kermit 7.0 / K95 1.1.19, you can write this more
  32. simply as:
  33.  
  34.   declare \&d[] = Sea urchin
  35.  
  36. : echo {\&d[1]}
  37. : echo {\&d[2]}
  38. : echo {Now try passing the array name to Test.}
  39. : Test \&d
  40. Sure, an array name can be passed as an argument to a macro.  The
  41. question is, how to refer to it inside the macro?  I confess, it's not
  42. obvious.
  43.  
  44. There are two problems with your example:
  45.  
  46. 1. "Test \&d" gives a syntax error.  You have to double the backslash,
  47.    or you could use:
  48.  
  49.       Test &a
  50.  
  51.     and apply the backslash inside the macro.
  52.  
  53.  2. But none of that helps, since "\%1[1]" is a compound construction.
  54.     When "\%1" is encountered, the variable expander is called to replace
  55.     it by its value, which is "\&d", and since this too starts with a
  56.     backslash, the variable expander is called again.  But "\&d" has no
  57.     value.
  58.  
  59. Here's a version of your Test macro that works:
  60.  
  61.   def test {
  62.     local \%x
  63.     .\%x := \\&\%1[1]
  64.     echo {\%x}
  65.     .\%x := \\&\%1[2]
  66.     echo {\%x}
  67.   }
  68.  
  69. First we construct the array reference string and assign it to a
  70. temporary variable, \%x, the kind that is always evaluated recursively
  71. (all the way down).  Then by referring to it, rather than directly to the
  72. constructed array element, we get the required full evaluation.
  73.  
  74. - Frank